package com.lkm.app_ui.m.Imple.hsj522;

import com.lkm.frame.P;
import com.lkm.frame.task.ProgressData;
import com.lkm.frame.task2.ITaskExecutor;
import com.lkm.frame.task2.Task;
import com.lkm.frame.task2.TaskAdapter;
import com.lkm.frame.task2.TaskManager;
import com.lkm.hardware_connect.IHardwareControler;
import com.lkm.javatool.NumberHelp;
import com.lkm.javatool.lang.RunnablePR;




@Deprecated
public class SendGetPaySingCMDTask extends TaskAdapter<Void,Void,InnerResponEntity<String>> {

    private IHardwareControler mIHardwareControler;
    private Task<?, ReadResponEntity, ?> mReadTask;
    public SendGetPaySingCMDTask(ITaskExecutor<Void, Void, InnerResponEntity<String>> taskExecutor, TaskManager mTaskManager, Task<?, ReadResponEntity, ?> mReadTask, IHardwareControler mIHardwareControler) {

        super(taskExecutor, SendGetPaySingCMDTask.class.getName(), mTaskManager);
        this.mIHardwareControler=mIHardwareControler;
        this.mReadTask=mReadTask;
    }


    private byte[] subBytes(byte[] src, int begin, int count) {
        byte[] bs = new byte[count];
        for (int i=begin; i<begin+count; i++) bs[i-begin] = src[i];
        return bs;
    }
    @Override
    public void doExecuting(Void param) {
        super.doExecuting(param);
        mTaskExecutor.executorDoAsyncExecuting(new RunnablePR<Void, Void>() {
            @Override
            public Void run(Void bluetoothSocket) {
                doAsyncExecuting(bluetoothSocket);
                return null;
            }
        },param);
    }
    public void doAsyncExecuting(Void param) {


        
        Task<?, ReadResponEntity, ?> readTask=mReadTask;
        readTask.addProgresInterceptor(new ProgresInterceptor<ReadResponEntity>() {
            @Override
            public boolean onInterceptProgresing(Task<?, ReadResponEntity, ?> task,ProgressData<ReadResponEntity> progressData) {


                return false;
            }

            @Override
            public boolean interceptProgressUpdate(Task<?, ReadResponEntity, ?> task,ProgressData<ReadResponEntity> data) {

                ReadResponEntity readResponEntity=data.getData();
                if(readResponEntity==null)return false;

                mReadTask.removeProgresInterceptor(this);
                if(readResponEntity.isSuccess()){

                    byte[] content=readResponEntity.getData();
                    String str=NumberHelp.bytesTohexDesc(content);


                    P.d("==== 不到内容"+str);
                    complete(str==null?new InnerResponEntity<String>().fail(null,""):new InnerResponEntity<String>().success(str,""));
                }else {
                    complete(new InnerResponEntity<String>().fail(null,""));
                }


                return false;
            }
        });

        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        boolean isSendSuccess=false;
        
        CMD_READ_CARD_NUM[CMD_READ_CARD_NUM.length-1-1]= HSJ800Help.crc((byte)0,(byte)0,CMD_READ_CARD_NUM);
        
        for (int i=0;i<3;i++) {
           if( verifyWrite(mIHardwareControler.write( CMD_READ_CARD_NUM))) {
               isSendSuccess=true;
               break;
           }
        }
        if(!isSendSuccess)
            complete(new InnerResponEntity<String>().fail(null,""));
    }


    

    public static byte[] CMD_READ_CARD_NUM =

    new byte[]{
            32
, 0
,  0x12
,  3
,   0x00
,  1
, (byte) HSJ800Help.getBlockNum(2,0)
,
            -21
, 3

    };

    private boolean verifyWrite(int ret) {
        if (ret <= 0) {


            return false;
        }
        return true;
    }
}
